package com.vmcs.entity.store.item.iterator;

import java.util.Arrays;
import java.util.Comparator;

import com.vcms.entity.store.item.StoreItem;
import com.vcms.entity.store.itemtype.Coin;

public class DenominationIterator extends CashStoreItemIterator {

	protected StoreItem items[];
	protected int current;

	public DenominationIterator(StoreItem[] items) {
		this.items = items;
		current = 0;

		this.sort();
	}
	
	protected void sort() {
		Arrays.sort(items, new Comparator<StoreItem>() {
			public int compare(StoreItem idx1, StoreItem idx2) {
				Coin coin1 = (Coin)idx1.getContent();
				Coin coin2 = (Coin)idx2.getContent();
				return coin2.getValue() - coin1.getValue();
			}
		});
	}

	@Override
	public StoreItem first() {
		current = 0;
		return null;
	}

	@Override
	public void next() {
		if (this.current() != null) {
			current++;
		}
	}

	@Override
	public Boolean isDone() {
		if (this.current() == null) {
			return true;
		}
		return false;
	}

	@Override
	public StoreItem current() {
		if (current == items.length) {
			return null;
		}
		return items[current];
	}

	public int getCurrent() {
		return current;
	}
}
